package org.molehill
{
	import flash.display.BitmapData;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DTextureFormat;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.textures.Texture;
	import flash.geom.Matrix3D;

	public class Role implements ISpriteMoleHill
	{
		private var _x:Number;
		private var _y:Number;
		private var _width:Number;
		private var _height:Number;
		private var _widthMolehill:int;
		private var _heightMolehill:int;
		private var _scaleX:Number;
		private var _scaleY:Number;
		private var _alpha:Number;
		private var _url:String;
		private var _prototype:String;
		public var viewMatrix:Matrix3D;
		public var direction:int;
		private var _xChange:int;
		
		private var _context:Context3D;
		private var _matrix:Matrix3D;
		private var _bitmapdata:BitmapData;
		private var _texture:Texture;
		public var indexBuffer:IndexBuffer3D;
		
		//public var 
		
		public function Role(context:Context3D,prototype:String)
		{
			_context    = context;
			_matrix     = new Matrix3D();
			_x = 0;
			_y = 0;
			_scaleX = 0;
			_scaleY = 0;
			_alpha = 1;
			_prototype = prototype;
			TexturePool.instance.regist(prototype,960,960);
			_xChange = 7*Math.random();
			direction = 7*Math.random();
		}
		
		private function getWorldTransform():Matrix3D
		{
			_matrix.identity();
			_matrix.appendScale(64, 64, 1);
			_matrix.appendTranslation(64 + x, 64 + y, 0);
			return _matrix;
		}
		private var flag:int;
		public function render():void{
			flag++;
			if(flag == 5){
				_xChange ++;
				if(_xChange == 8){
					_xChange = 0;
				}
				flag = 0;
			}
			var mvp:Matrix3D = this.getWorldTransform();
			mvp.append(viewMatrix);
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, mvp, true);
			_context.setTextureAt(1, TexturePool.instance.getRoleTexture(_prototype + "*" + _xChange + "_"+ direction ));
			_context.drawTriangles(indexBuffer, 0, 2);
		}
				
		public function set alpha(value:Number):void
		{
			this._alpha = value;
		}
		
		public function get alpha():Number
		{
			return this._alpha;
		}
		
		public function get height():Number
		{
			return this._height;
		}
		
		public function get width():Number
		{
			return this._width;
		}
		
		public function set x(value:Number):void
		{
			this._x = value;
		}
		
		public function get x():Number
		{
			return this._x;
		}
		
		public function set y(value:Number):void
		{
			this._y = value;
		}
		
		public function get y():Number
		{
			return this._y;
		}

		public function set url(value:String):void
		{
			_url = value;
		}


	}
}